package org.example;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class Test1 {
    //leetcode 对称二叉树
    //https://leetcode.cn/problems/symmetric-tree/description/?envType=problem-list-v2&envId=tree

    /**
     * 非递归方式
     * @param root
     * @return
     */
    public boolean isSymmetric1(TreeNode root) {
        //保证deque中是按照root左子树的左孩子，root右子树的右孩子，
        //root左子树的右孩子，root右子树的左孩子的顺序插入
        if (root == null) return true;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.add(root.left);
        deque.add(root.right);
        while (!deque.isEmpty()) {
            TreeNode leftNode = deque.poll();
            TreeNode rightNode = deque.poll();
            if (leftNode == null && rightNode == null) continue;
            if (leftNode == null || rightNode == null|| leftNode.val != rightNode.val) {
                return false;
            }
            deque.add(leftNode.left);
            deque.add(rightNode.right);
            deque.add(leftNode.right);
            deque.add(rightNode.left);
        }
        return true;
    }

    /**
     * 递归实现
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        return isChildSymmetric(root.left,root.right);
    }

    private boolean isChildSymmetric(TreeNode leftNode,TreeNode rightNode) {
        if (leftNode == null && rightNode == null) return true;
        if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
            return false;
        }
        return isChildSymmetric(leftNode.left,rightNode.right)
                && isChildSymmetric(leftNode.right,rightNode.left);
    }

    public static void main(String[] args) {
        Deque<Integer> deque= new ArrayDeque<>();
        deque.isEmpty();
    }
}
